﻿using JZ.JiangSu.Common.DependencyInjection;
using JZ.JiangSu.WcfService.IWcfServices;
using JZ.JiangSu.WcfService.WcfServices;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Linq;
using System.Reflection;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.Text;
using System.Threading.Tasks;
using Unity;

namespace JZ.JiangSu.Services.Loader
{
    class Program
    {
        static void Main(string[] args)
        {
            //直接测试服务方法
            //IUserService companyInfoService = new UserService();
            //IUserService companyInfoService = IocContainerProvider.IocContainer.Resolve<IUserService>();

            //var list = companyInfoService.GetTopVipCompany(5);

            //Console.WriteLine(list.Count);
            //Console.ReadLine();
            //return;
            //end test


            //var host = new ServiceHost(typeof(UserService));
            //host.Open();
            //Console.WriteLine("service is ready.");
            //Console.WriteLine("Press <ENTER> to terminate service.");
            //Console.WriteLine();
            //Console.ReadLine();

            var WcfService = ConfigurationManager.AppSettings["WcfService"];
            var services = WcfService.Split(',');
            if (services.Length > 0)
            {
                List<ServiceHost> hosts = new List<ServiceHost>();
                try
                {
                    Assembly asmb = Assembly.LoadFrom("JZ.JiangSu.WcfService.dll");
                    foreach (var service in services)
                    {
                        var host = new ServiceHost(asmb.GetType("JZ.JiangSu.WcfService.WcfServices." + service));
                        host.Open();
                        Console.WriteLine($"{service} is ready.");
                        hosts.Add(host);
                    }
                }
                catch (CommunicationException ce)
                {
                    Console.WriteLine("An exception occurred: {0}", ce.Message);
                    foreach (var host in hosts)
                    {
                        host.Abort();
                    }
                }
                Console.WriteLine("all of services are ready.");
                Console.WriteLine("Press <ENTER> to terminate service.");
                Console.WriteLine();
                Console.ReadLine();
            }
        }
    }
}
